package com.yellowpage.ca.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.ca.data.UserFunctionConfig;
import com.yellowpage.ca.dao.UserFunctionConfigDao;

public class UserFunctionConfigServiceImpl extends BaseServiceImpl implements UserFunctionConfigService {
	  private UserFunctionConfigDao userFunctionConfigDao;
	
	  public void setUserFunctionConfigDao(UserFunctionConfigDao userFunctionConfigDao) {
		  this.userFunctionConfigDao = userFunctionConfigDao;
	  }
	
	
	  /* Smooth generate Method */

	public UserFunctionConfig getUserFunctionConfig(Long id)

	  {
	      return userFunctionConfigDao.get(id);
	  }
	  
    public List<UserFunctionConfig> getAllUserFunctionConfig(){
        return userFunctionConfigDao.getAll();
    }
    
    public List<Map> queryUserFunctionConfigByPage(Map queryMap, PageRender pageRender) {
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(

				"SELECT a.VALUE , a.CODE , a.CONFIG_ID , a.USER_FUNCTION_ID, " +

						" date_format(a.CREATE_TIME,'%Y-%m-%d %T') as CREATE_TIME, " +


						//给出合适的姓名，最后更新者
						"  (select display_name from CA_USER where user_id = a.LAST_UPDATE_USER) as LAST_UPDATE_USER, " +

						//给出合适姓名，创建者
						"  (select display_name from CA_USER where user_id = a.CREATE_USER) as CREATE_USER, " +

						//规范日期格式
						"  date_format(a.LAST_UPDATE_TIME,'%Y-%m-%d %T') as LAST_UPDATE_TIME "

						+ "  FROM CA_USER_FUNCTION_CONFIG a WHERE 1=1 ")


											.addCondition(" and VALUE = :value ")
							    			.addCondition(" and  (select display_name from CA_USER where user_id = a.CREATE_USER) like :createUser ")
							    		    .addCondition(" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ")
		    				    			.addCondition(" and CODE = :code ")
							    			.addCondition(" and CONFIG_ID = :configId ")
							    			.addCondition(" and LAST_UPDATE_USER = :lastUpdateUser ")
							    		    .addCondition(" and date_format(LAST_UPDATE_TIME,'%Y-%m-%d') =  :lastUpdateTime  ");


		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);


		QueryAssistantSQL sqlAssistant2 = baseDao
				.newQueryAssistantSQL(

						"  select f.USER_FUNCTION_ID as USER_FUNCTION_ID ,  " +

								" (select DISPLAY_NAME from CA_USER Where CA_USER.USER_ID = f.USER_ID) as NAME, " +
								" (select NAME from CA_FUNCTION Where CA_FUNCTION.FUNCTION_ID = f.FUNCTION_ID) as FUNCTION " +
								"  from CA_USER_FUNCTION f ")
								.addCondition(" and USER_FUNCTION_ID like :userFunction ");

		//给出USEFUCTION_ID的结果
		List<Map> userfunctionid = sqlAssistant2.query(baseDao.getConnection(),
				new HashMap());


		for (int i = 0; i < resultList.size(); i++)
		{


				//果加入resultlist
				for (int j = 0; j < userfunctionid.size(); j++)
				{
					//确保userfunctionid与resultlist匹配
					//相等符号表示了绝对的相等
					if ( resultList.get(i).get("USER_FUNCTION_ID").toString().equals(userfunctionid.get(j).get("USER_FUNCTION_ID").toString()))
					{
						resultList.get(i).put("USER_FUNCTION_ID",
								(userfunctionid.get(j).get("NAME") + " " + userfunctionid.get(j).get("FUNCTION")));
					}
				}
		}


		return resultList;
	}
    

	//进行userFunciton存储，
    public void saveUserFunctionConfig(UserFunctionConfig userFunctionConfig,Integer id)

	{
		//写入创建时间
		userFunctionConfig.setCreateTime(new Date());

		//创建时候一定是最后修改时间
		userFunctionConfig.setLastUpdateTime(new Date());

		//写入创建者
		userFunctionConfig.setCreateUser(id);

		//第一次写入,作者一定是最后更新者
		userFunctionConfig.setLastUpdateUser(id);

		userFunctionConfigDao.save(userFunctionConfig);
    }
    
	public void updateUserFunctionConfig(UserFunctionConfig userFunctionConfig)
	{

		  //创建时候一定是最后修改时间
		  userFunctionConfig.setLastUpdateTime(new Date());

		  //第一次写入,作者一定是最后更新者
		  //userFunctionConfig.setLastUpdateUser(id);

	      userFunctionConfigDao.update(userFunctionConfig);
	}
	  
	  public void delete(List deleteList){
	      userFunctionConfigDao.remove(deleteList);
	  }

	//获得用户功能表对应字段
	public Map getuserfunctionmap()
	{
		Map userfunctionmap = new LinkedMap();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(

						        "  select f.USER_FUNCTION_ID as ID,  " +
								" (select DISPLAY_NAME from CA_USER Where CA_USER.USER_ID = f.USER_ID) as NAME, " +
								" (select NAME from CA_FUNCTION Where CA_FUNCTION.FUNCTION_ID = f.FUNCTION_ID) as FUNCTION  "  +
										"  from CA_USER_FUNCTION f "
						      );

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());



		for (int i = 0; i < resultList.size(); i++)
		{
			//最终的字符串拼接为map(userfunciton1,用户名 +功能名）
			userfunctionmap.put(resultList.get(i).get("ID"),
					(resultList.get(i).get("NAME") + " " + resultList.get(i).get("FUNCTION")) );
		}

		return userfunctionmap;
	}
	  
	  
	 
	
}